﻿using System;
using System.Data;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace Pos.DAL
{
    public class DataHelper
    {
        public static Int16? GetInt16(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToInt16(obj);
            }
            else
            {
                return null;
            }
        }

        public static Int32 GetInt32(object obj)
        {
          
             return Convert.ToInt32(obj);
         
        }

        public static UInt16? GetUInt16(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToUInt16(obj);
            }
            else
            {
                return null;
            }
        }

        public static int? GetInt(object obj)
        {
            if (obj != DBNull.Value && obj != null && obj.ToString() != "")
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return null;
            }
        }

        public static uint? GetUInt(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToUInt32(obj);
            }
            else
            {
                return null;
            }
        }

        public static ulong? GetULong(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToUInt64(obj);
            }
            else
            {
                return null;
            }
        }

        public static Byte? GetByte(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToByte(obj);
            }
            else
            {
                return null;
            }
        }

        public static sbyte? GetSByte(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToSByte(obj);
            }
            else
            {
                return null;
            }
        }

        public static long? GetLong(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToInt64(obj);
            }
            else
            {
                return null;
            }
        }

        public static decimal? GetDecimal(object obj)
        {
            if (obj != DBNull.Value && obj != null && obj.ToString() != "")
            {
                return Convert.ToDecimal(obj);
            }
            else
            {
                return null;
            }
        }

        public static float? GetFloat(object obj)
        {
            if (obj != DBNull.Value && obj != null && obj.ToString() != "")
            {
                return Convert.ToSingle(obj);
            }
            else
            {
                return null;
            }
        }

        public static double? GetDouble(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToDouble(obj);
            }
            else
            {
                return null;
            }
        }

        public static Guid? GetGuid(object obj)
        {
            if (obj != DBNull.Value)
            {
                return new Guid(obj.ToString());
            }
            else
            {
                return null;
            }
        }

        public static DateTime? GetDateTime(object obj)
        {
            if (obj == null)
                return null;

            if (obj != DBNull.Value)
            {
                DateTime tmp;
                if (DateTime.TryParse(obj.ToString(), out tmp))
                {
                    return tmp;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static bool GetBool(object obj)
        {
            if (obj != DBNull.Value)
            {
                return Convert.ToBoolean(obj);
            }
            else
            {
                return false;
            }

        }

        public static Byte[] GetBinary(object obj)
        {
            if (obj != DBNull.Value)
            {
                return (Byte[])obj;
            }
            else
            {
                return null;
            }
        }

        public static string GetString(object obj)
        {
            return obj.ToString();
        }

        public static object GetDBGuid(string obj)
        {
            if (!string.IsNullOrEmpty(obj))
                return new Guid(obj);
            return DBNull.Value;
        }
    }
}
